package com.hjm.admin.moule.view.fragment.impl;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hjm.admin.moule.R;
import com.hjm.admin.moule.api.ModelPath;
import com.hjm.admin.moule.model.BookBean;
import com.hjm.admin.moule.view.activity.impl.BookDetailActivity;
import com.hjm.admin.moule.view.adapter.BookInfoAdapter;
import com.hjm.admin.moule.view.base.BaseFragment;
import com.hjm.admin.moule.view.fragment.IBookInfo;
import com.hjm.admin.moule.viewmodel.fragment.VMBooksInfo;
import com.hjm.admin.moule.widget.SpacesItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.scwang.smartrefresh.layout.util.DensityUtil;
import com.weavey.loading.lib.LoadingLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class BooksInfoFragment extends BaseFragment implements IBookInfo {
    @BindView(R.id.rv_bookinfo)
    RecyclerView mRvBookinfo;
    @BindView(R.id.refresh)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.loadinglayout)
    LoadingLayout mLoadingLayout;

    String titleName;
    String gender;
    String type;
    private VMBooksInfo mModel;
    private List<BookBean> mBookBeans;
    private int loadPage = 1;
    private BookInfoAdapter mBookInfoAdapter;

    public static BooksInfoFragment newInstance(String titleName, String gender, String type) {
        Bundle bundle = new Bundle();
        bundle.putString("titleName", titleName);
        bundle.putString("gender", gender);
        bundle.putString("type", type);
        BooksInfoFragment booksInfoFragment = new BooksInfoFragment();
        booksInfoFragment.setArguments(bundle);
        return booksInfoFragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mModel = new VMBooksInfo(mContext, this);
        return setContentView(container, R.layout.fragment_book_info, mModel);
    }

    @Override
    protected void initView() {
        titleName = getArguments().getString("titleName");
        gender = getArguments().getString("gender");
        type = getArguments().getString("type");
        mBookBeans=new ArrayList<>();
        mRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
               loadPage++;
                mModel.getBooks(type, titleName, loadPage);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                loadPage = 1;
                mModel.getBooks(type, titleName, loadPage);
            }
        });
        mRefreshLayout.autoRefresh();
        mLoadingLayout.setOnReloadListener(v -> mModel.getBooks(type, titleName, loadPage));
        mBookInfoAdapter = new BookInfoAdapter(mBookBeans);
        mRvBookinfo.setLayoutManager(new LinearLayoutManager(mContext));
        mRvBookinfo.addItemDecoration(new SpacesItemDecoration(DensityUtil.dp2px(10)));
        mRvBookinfo.setAdapter(mBookInfoAdapter);
        mBookInfoAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(mContext, BookDetailActivity.class);
            intent.putExtra("bookid", mBookBeans.get(position).get_id());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                ImageView imageView = view.findViewById(R.id.book_brief_iv_portrait);
                startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(getActivity(), imageView, "bookImage").toBundle());
            } else {
                startActivity(intent);
            }
        });
    }

    @Override
    public void getBooks(List<BookBean> bookBeans, boolean isLoadMore) {
        if (!isLoadMore) {
            mBookBeans.clear();
        }
        mBookBeans.addAll(bookBeans);
        mBookInfoAdapter.notifyDataSetChanged();
    }

    @Override
    public void emptyData() {
        mLoadingLayout.setStatus(LoadingLayout.Empty);
    }

    @Override
    public void errorData(String error) {
        mLoadingLayout.setErrorText(error).setStatus(LoadingLayout.Error);
    }

    @Override
    public void netWorkError() {
        mLoadingLayout.setStatus(LoadingLayout.No_Network);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void stopLoading() {
        mRefreshLayout.finishRefresh();
        mRefreshLayout.finishLoadmore();
    }
}
